{extend name="base" /} {block name="css"}
<link rel="stylesheet" href="/public/static/css/ztree-metro-style.css"> {/block} {block name="body"}
<div class="layui-body">
	<div class="navbar">
		<a><cite>系统设置</cite></a>&nbsp;<span class="layui-box">&gt;</span>
		<a href="{:url('index')}"><cite>权限组</cite></a>&nbsp;<span class="layui-box">&gt;</span>
		<a href=""><cite>授权</cite></a>
	</div>
	<!--tab标签-->
	<div class="layui-tab layui-tab-card margin10">
		<ul class="layui-tab-title">
			<li class="layui-this">授权</li>
		</ul>
		<div class="layui-tab-content">
			<ul id="tree" class="ztree"></ul>
			<div class="layui-input-block">
				<input type="hidden" id="group_id" name="id" value="{$id}">
				<button class="layui-btn" id="auth-btn">保存授权</button>
				<a href="{:url('index')}" class="layui-btn layui-btn-primary"><i class="fa fa-arrow-left"></i> 返回</a>
			</div>
		</div>
	</div>
</div>
{/block} {block name="js"}
<script src="/public/static/js/jquery.ztree.all.min.js"></script>
{/block} {block name="script"}
<script>
	$(document).ready(function() {
		/**
		 * 加载树形授权菜单
		 */
		var _id = $("#group_id").val();
		var tree = $("#tree");
		var zTree;

		// zTree 配置项
		var setting = {
			check: {
				enable: true
			},
			view: {
				dblClickExpand: false,
				showLine: true,
				showIcon: false,
				selectedMulti: false
			},
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pid",
					rootpid: ""
				},
				key: {
					name: "title"
				}
			}
		};

		$.ajax({
			url: "{:url('auth_group/getJson')}",
			type: "post",
			dataType: "json",
			cache: false,
			data: {
				id: _id
			},
			success: function(data) {
				zTree = $.fn.zTree.init(tree, setting, data);
			}
		});

		/**
		 * 授权提交
		 */
		$("#auth-btn").on("click", function() {
			var checked_ids, auth_rule_ids = [];
			checked_ids = zTree.getCheckedNodes(); // 获取当前选中的checkbox
			$.each(checked_ids, function(index, item) {
				auth_rule_ids.push(item.id);
			});
			$.ajax({
				url: "{:url()}",
				type: "post",
				cache: false,
				data: {
					id: _id,
					auth_rule_ids: auth_rule_ids
				},
				success: function(data) {
					if(data.code === 1) {
						setTimeout(function() {
							location.href = data.url;
						}, 1000);
					}
					layer.msg(data.msg);
				}
			});
		});
	});
</script>
{/block}